<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="addDept('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row> <!--icon-class 等于空 去除默认图标 -->
        <el-tree :data="list" default-expand-all icon-class=" ">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template slot-scope="{ data,node }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <!-- --{{data}} icon-class="el-icon-circle-plus" -->
              <el-col :span="20">
                <span><svg-icon :icon-class="node.expanded?'link':'table'" />{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addDept(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="editDept(data.id)"
                        >编辑部门</el-dropdown-item>
                        <!-- v-if="data.children.length===0" -->
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="delDept(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- 添加或编辑弹出层 -->
        <el-dialog
          :title="isEdit ? '编辑' : '添加'"
          :visible.sync="showDialog"
          @close="close"
        >
          <DeptDialog
            :id="curID"
            ref="deptForm"
            :origin-list="originList"
            :is-edit="isEdit"
            @success="hSuccess"
            @edit="hEdit"
            @closeDialog="cDia"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import DeptDialog from './deptDialog.vue'
export default {
  name: 'Department',
  components: {
    DeptDialog
  },
  data() {
    return {
      list: [],
      // 是否处于编辑状态
      isEdit: false,
      // 当前id
      curID: '',
      // 弹层是否显示
      showDialog: false,
      // 要校验的数组
      originList: []
    }
  },
  created() {
    this.loaadDepartments()
  },
  methods: {
    // 获取部门所有信息
    async loaadDepartments() {
      const res = await getDepartments()
      // console.log(res)
      // 数据结构不需要数组中第一个元素，所以要删除
      res.data.depts.shift()
      // 改变要校验的数据
      this.originList = res.data.depts.map((it) => {
        return { id: it.id, pid: it.pid, code: it.code, name: it.name }
      })
      console.log(this.originList)
      this.list = res.data.depts
      // console.log(this.list)
      // 数组转树
      tranListToTreeData(this.list)
      //   alert(this.list)
    },
    // 添加部门
    addDept(id) {
      // 打开弹层
      this.showDialog = true
      // 更新当前id
      this.curID = id
      // 更新状态
      this.isEdit = false
    },
    // 编辑部门
    editDept(id) {
      // 打开弹层
      this.showDialog = true
      // 更新id
      this.curID = id
      // 更新状态
      this.isEdit = true
      // 等到dom更新完毕再执行
      this.$nextTick(() => {
        this.$refs.deptForm.loadDepartments()
      })
    },
    // 成功添加
    hSuccess() {
      // 关闭弹层
      this.showDialog = false
      // // 更新页面
      this.loaadDepartments()
    },
    // 成功编辑
    hEdit() {
      // 关闭弹层
      this.showDialog = false
      // // 更新页面
      this.loaadDepartments()
    },
    // 删除部门
    async delDept(id) {
      const res = await delDepartments(id)
      console.log(res)
      // 刷新页面
      this.loaadDepartments()
    },
    // 用户点取消要关闭弹层
    cDia() {
      this.showDialog = false
    },
    // 用户关闭弹层
    close() {
      // this.$refs.deptForm.resetField()
      this.$refs.deptForm.resetForm()
    }
  }
}
</script>

<style>
</style>
